package org.example;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.function.IntBinaryOperator;
import java.util.function.IntPredicate;

/**
 * @author shenguangyang
 */
public class LambdaTest {

    @Test
    public void test01() {
        System.out.println(calculateNum(Integer::sum));
        System.out.println(calculateNum(Integer::min));
        System.out.println(calculateNum(Integer::max));
    }

    @Test
    public void test02() {
        printNum((data) -> data % 2 == 0);
        System.out.println("------------------------------");
        printNumOfLambda((data) -> data % 2 == 0);
    }

    public int calculateNum(IntBinaryOperator operator) {
        int a = 10;
        int b = 20;
        return operator.applyAsInt(a, b);
    }

    /**
     * 没有使用lambda
     */
    public void printNum(IntPredicate intPredicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            if (intPredicate.test(i)) {
                System.out.println(i);
            }
        }
    }

    public void printNumOfLambda(IntPredicate intPredicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        Arrays.stream(arr).filter(intPredicate).forEach(System.out::println);
    }
}
